package com.sxj.shop.service.proxy;

import com.sxj.shop.entity.Product;
import com.sxj.shop.exception.ProductNotExistException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.ProductService;
import com.sxj.shop.tran.TransactionManager;
import com.sxj.shop.util.PageInfo;

import java.util.List;

public class ProductServiceProxy implements ProductService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");

    @Override
    public List<Product> findAll() {
        tran.begin();
        List<Product> products = productService.findAll();
        tran.commit();
        return products;
    }

    @Override
    public PageInfo<Product> findPage(String pageNo, String pageSize) throws ProductNotExistException {
        try {
            tran.begin();
            PageInfo<Product> productPage = productService.findPage(pageNo, pageSize);
            tran.commit();
            return productPage;
        } catch (ProductNotExistException e) {
            tran.commit();
            throw e;
        }
    }

//    @Override
//    public Product findById(String id) throws RequestParameterErrorException {
//        try {
//            tran.begin();
//            Product product = productService.findById(id);
//            tran.commit();
//            return product;
//        } catch (RequestParameterErrorException e) {
//            tran.rollback();
//            throw e;
//        }
//    }
}
